package models;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import play.db.jpa.Model;

/**
 * @author Heba
 */
/*
 * This is the class of the Departments in an Organization I renamed it from
 * Entity to Department because, of the conflict between Entity and @Entity.
 */

@Entity
public class Department extends Model {
	public String name;
    public boolean isLocked;
	@OneToMany(cascade = CascadeType.ALL)
	public List<Topic> topics;
	public String desc;
    public int views;
    @ManyToMany
    public List<User> toBeNotified;
    @OneToMany
	public List<PromotedIdea> executionCenter;

	
	public Department(String name, String desc) {

		this.name = name;
		this.desc = desc;
		toBeNotified = new ArrayList<User>();
		this.executionCenter=new ArrayList<PromotedIdea>();

	}

	/**
	 * @author Khalid Osama
	 * @param user to be added to the list of followers.
	 * this method adds the new user to the list of followers.
	 */
	public void addFollowers(User user) {
			toBeNotified.add(user);
	}

	public void addTopic(Topic topic) {
		this.topics.add(topic);
	}
	
	/**
	 * @author Khalid Osama
	 * @param user to check whether following or not.
	 * this method checks whether the user is following or not.
	 * @return true if following, false if not.
	 */
	public boolean isFollowing(User user){
		if (toBeNotified.contains(user)) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * @author Khalid Ramzy
	 * @param TopicList  to have the list of topics
	 */
	/*
	 * the method task is to determine wether the topic is active or not
	 */
	public static int topicCompare(Long did){
	   Department d =Department.findById(did);
	   List<Topic> Topiclist=d.topics;
		System.out.println("we entered the method");
		int i=0; // counter for topics
		int j=0; // counter for ideas
		int k=Topiclist.size();
		int l=0;
		int numberofideas=11; // number of topics under search
		Date ideatime;
		Date currenttime=new Date(); 
		Date lastactivity;// the last activity done in the topic
		int activeidea=0;
		int inactiveidea=0;
		
		
		while(i<k){
			System.out.println("we enterd the big while");

	    	Topic y= Topiclist.get(i); // choosing a topic
	    	lastactivity=y.lastactivity;
	    	    	
	    	
			if (timeCompare(did,i,j)==false){
			    i++;
				return 1;
			}
			else{
				 l=y.ideas.size();
	        	while(j<l&&j<numberofideas){  
	    			System.out.println("inside the 2nd loop"+j);

				ideatime=y.ideas.get(j).idealastactivity;
			   if(timeCompare(did,i,j)==false)
			    	inactiveidea++;
			   else 
			    	activeidea++;
			    j++;
			   }
			
			//determination of topic activity state based on avarage technique
			if (activeidea-inactiveidea<0){	
	        return 1;
			}

			
	        // proceeding with the following topic!
			j=0;  inactiveidea=0;  activeidea=0; i++;
		}
	}
		return 3;
	}

	/**
	 * @author Khalid Ramzy
	 * @param ideatime
	 * @param currenttime

	 * 
	 */
	/*
	 * The methods takes two dates and compare them and return boolean
	 * result used in topicCompare() to define the activity of the topic
	 */
	public static boolean timeCompare(Long did,int i ,int j){
		 Department d =Department.findById(did);
		  List<Topic> Topiclist=d.topics;
		  int lasti=i-1;
		  Date x=new Date();
		  //int f=Integer.parseInt(("m"+(Topiclist.size()+"")));
		  if (j!=0 && i==lasti){
		   
		   Date date=Topiclist.get(0).ideas.get(j).idealastactivity;
		//   x=DateFormat.getInstance().format(date);
		   x = date;
		  }
		  else{
		   Date date=Topiclist.get(0).lastactivity;
		//   x=DateFormat.getInstance().format(date);
		   x = date;
		  }

		  Date y=new Date();
		  //String x=DateFormat.getInstance().format(activitydate)+activitydate.toGMTString();

		  int timedifference=1;
		  int maxofmonth=0;
		  boolean result = false;
		  
		//  int n=Integer.parseInt((y));
		  int day1=x.getDay();
		  int month1=x.getDate();
		  int year1=x.getYear();
		        
		         
		  int day2=y.getDay();
		  int month2=y.getDate();
		  int year2=y.getYear();
		  
		  if(year1==year2&&month1==month2){

		   if(day2-day1>timedifference)
		        result= false;
		   else result= true;
		  }

		  if(year1==year2&&month1!=month2){
		    if(month2-month1==1){
		     
		    switch( month1){
		           case 1: case 3: case 5:case 7: case 8: case 10: case 12:  maxofmonth=31 ;break; 
		           case 2:                                                   maxofmonth=28 ;break; 
		              case 4: case 6: case 9: case 11:                          maxofmonth=30 ;break;
		     }
		    
		   if(maxofmonth-day1+day2<timedifference)
		        result= true;
		     else result= false;
		    }
		    else result= false;
		     }

		  if(year2>year1)
		   result= false;
		  
		  return result;	
	}
}